<?php
declare(strict_types=1);

namespace App\Controller;

use App\Service\PlayletService;
use App\Service\UserService;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use App\Service\RewardService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Contract\ResponseInterface;

#[AutoController]
class RewardRecordsController extends BaseController
{
    #[Inject]
    private RewardService $rewardService;

    #[Inject]
    private UserService $userService;
    #[Inject]
    private PlayletService $playletService;


    #iaa奖励创建
    public function start(RequestInterface $request,ResponseInterface $response)
    {
        $applet_id = $this->get_applet_id();
        $xc_open_id = $request->input("xc_open_id","");
        $playlet_id = $request->input("playlet_id","");
        $playlet_number = $request->input("playlet_number","");
        #观看广告时间
        $start_time = $request->input("start_time",time());
        $start_time = date('Y-m-d H:i:s',intval($start_time));
        #广告位id
        $ad_unit_id = $request->input("ad_unit_id","");
        $open_id_info = $this->userService->get_xc_open_id_info($applet_id,$xc_open_id);
        $user_id = !empty($open_id_info['id']) ? $open_id_info['id'] : "";
        if (empty($user_id)){
            return $this->resultResponse(["code"=>20001]);
        }
        if (empty($playlet_id) or empty($playlet_number)or empty($ad_unit_id)){
            return $this->resultResponse(["code"=>20060]);
        }
        $insert['applet_id'] = $applet_id;
        $insert['user_id'] = $user_id;
        $insert['open_id'] = $xc_open_id;
        $insert['playlet_id'] = $playlet_id;
        $insert['playlet_number'] = $playlet_number;
        $insert['create_time'] = date('Y-m-d H:i:s',time());
        $insert['update_time'] = date('Y-m-d H:i:s',time());
        $insert['start_time'] = $start_time;
        $insert['ad_unit_id'] = $ad_unit_id;
        $order_id = $this->rewardService->reward_records_insert($insert);
        return $this->resultResponse(["data"=>['order_id'=>$order_id]]);
    }
    #iaa奖励发放
    public function complete(RequestInterface $request,ResponseInterface $response) {
        $order_id = $request->input("order_id","");
        $type = $request->input("type","3");
        $sign = $request->input("sign","");
        $end_time = $request->input("end_time",time());
        $ymd_end_time = date('Y-m-d H:i:s',intval($end_time));
        #验证sign
        $check = $this->rewardService->chech_sign($sign,$order_id,$end_time);
        if(!$check) {
            return $this->resultResponse(["code"=>20062]);
        }
        $isTrue = false;
        #验证order_id是否存在
        if (empty($order_id)){
            return $this->resultResponse(["code"=>20063]);
        }
        $order_info = $this->rewardService->get_order($order_id);
        if (empty($order_info)){
            return $this->resultResponse(["code"=>20063]);
        }
        #发货
        if($type == 2) {
            $isTrue = $this->rewardService->sendGoods($order_info);
        }
        #修改状态
        $this->rewardService->update_reward_records($order_info,$ymd_end_time,$type);
        if($isTrue) {
            $this->playletService->up_my_playlet_episodes($order_info->applet_id,$order_info->user_id,$order_info->playlet_id);
            return $this->resultResponse(["data"=>[]]);
        } else {
            return $this->resultResponse(["code"=>20001]);
        }
    }

}